package com.bkgy.modular.calsidata.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.calsidata.entity.Evaluate;
import com.bkgy.modular.calsidata.entity.Evaluate_thk;
import com.bkgy.modular.calsidata.entity.PlanprdList;
import com.bkgy.modular.calsidata.entity.Planwtpredict;
import com.bkgy.modular.calsidata.mapper.PlanprodListDao;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EvaluateService {
    @Resource
    private MongoTemplate mongoTemplate;//注入自带的查询工具

    @Resource
    private PlanprodListDao planprodListDao;

    @Resource
    private PlanprodListDao planProdListMapper;

    /**
     * 不分页查看记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataEvaluateNoPage(String data) {
        try {
            Evaluate evaluate = JSON.parseObject(data, Evaluate.class);
            Query query = new Query();

            String extid = null;
            if (!StringUtils.isEmpty(evaluate.getExtid()) && null != evaluate.getExtid()) {
                extid = evaluate.getExtid();
                query.addCriteria(Criteria.where("extid").regex(extid));
            }

            Evaluate_thk thk = new Evaluate_thk();
            List<Evaluate> listx = mongoTemplate.find(query, Evaluate.class);
            if (listx.size() != 0 && !StringUtils.isEmpty(listx)) {
                for (Evaluate entity : listx) {
                    String exid = entity.getExtid();
                    String THK_WS_Tole_Pos = entity.getTHK_WS_Tole_Pos();
                    String THK_Mid_Tole_Pos = entity.getTHK_Mid_Tole_Pos();
                    String THK_DS_Tole_Pos = entity.getTHK_DS_Tole_Pos();
                    String THK_Whole_Tole_Pos = entity.getTHK_Whole_Tole_Pos();
                    String edge_WS_Tole_Pos = entity.getEdge_WS_Tole_Pos();
                    String edge_DS_Tole_Pos = entity.getEdge_DS_Tole_Pos();
                    String edge_Whole_Tole_Pos = entity.getEdge_Whole_Tole_Pos();
                    String samePlate10_WS_Tole_Pos = entity.getSamePlate10_WS_Tole_Pos();
                    String samePlate10_DS_Tole_Pos = entity.getSamePlate10_DS_Tole_Pos();
                    String samePlate10_Whole_Tole_Pos = entity.getSamePlate10_Whole_Tole_Pos();
                    String samePlate15_WS_Tole_Pos = entity.getSamePlate15_WS_Tole_Pos();
                    String samePlate15_DS_Tole_Pos = entity.getSamePlate15_DS_Tole_Pos();
                    String samePlate15_Whole_Tole_Pos = entity.getSamePlate15_Whole_Tole_Pos();

                    Map<String, List<Double>> maps = entity.getEdge();
                    for (Map.Entry<String, List<Double>> entry : maps.entrySet()) {
                        String key1 = entry.getKey();
                        List<Double> value1 = entry.getValue();
                        thk.setExid(exid);
                        thk.setTHK_WS_Tole_Pos(THK_WS_Tole_Pos);
                        thk.setTHK_Mid_Tole_Pos(THK_Mid_Tole_Pos);
                        thk.setTHK_DS_Tole_Pos(THK_DS_Tole_Pos);
                        thk.setTHK_Whole_Tole_Pos(THK_Whole_Tole_Pos);
                        thk.setEdge_WS_Tole_Pos(edge_WS_Tole_Pos);
                        thk.setEdge_DS_Tole_Pos(edge_DS_Tole_Pos);
                        thk.setEdge_Whole_Tole_Pos(edge_Whole_Tole_Pos);
                        thk.setSamePlate10_WS_Tole_Pos(samePlate10_WS_Tole_Pos);
                        thk.setSamePlate10_DS_Tole_Pos(samePlate10_DS_Tole_Pos);
                        thk.setSamePlate10_Whole_Tole_Pos(samePlate10_Whole_Tole_Pos);
                        thk.setSamePlate15_WS_Tole_Pos(samePlate15_WS_Tole_Pos);
                        thk.setSamePlate15_DS_Tole_Pos(samePlate15_DS_Tole_Pos);
                        thk.setSamePlate15_Whole_Tole_Pos(samePlate15_Whole_Tole_Pos);

                        if ("whole_thk_grade".equals(key1)) {
                            thk.setWhole_thk_grade(value1);
                        } else if ("EDM_DS".equals(key1)) {
                            thk.setEDM_DS(value1);
                        } else if ("EDM_WS".equals(key1)) {
                            thk.setEDM_WS(value1);
                        }else if ("EDM_DS_10".equals(key1)) {
                            thk.setEDM_DS_10(value1);
                        } else if ("EDM_WS_10".equals(key1)) {
                            thk.setEDM_WS_10(value1);
                        } else if ("EDM_DS_5".equals(key1)) {
                            thk.setEDM_DS_5(value1);
                        } else if ("EDM_WS_5".equals(key1)) {
                            thk.setEDM_WS_5(value1);
                        }else if ("samePlate_ds5".equals(key1)) {
                            thk.setSamePlate_ds5(value1);
                        } else if ("samePlate_ws5".equals(key1)) {
                            thk.setSamePlate_ws5(value1);
                        } else if ("samePlate_ds10".equals(key1)) {
                            thk.setSamePlate_ds10(value1);
                        } else if ("samePlate_ws10".equals(key1)) {
                            thk.setSamePlate_ws10(value1);
                        } else if ("samePlate_ds15".equals(key1)) {
                            thk.setSamePlate_ds15(value1);
                        } else if ("samePlate_ws15".equals(key1)) {
                            thk.setSamePlate_ws15(value1);
                        } else if ("whole_edge_ds_grade".equals(key1)) {
                            thk.setWhole_edge_ds_grade(value1);
                        } else if ("whole_edge_ws_grade".equals(key1)) {
                            thk.setWhole_edge_ws_grade(value1);
                        } else if ("whole_samePlate10_ds_grade".equals(key1)) {
                            thk.setWhole_samePlate10_ds_grade(value1);
                        } else if ("whole_samePlate10_ws_grade".equals(key1)) {
                            thk.setWhole_samePlate10_ws_grade(value1);
                        } else if ("whole_samePlate15_ds_grade".equals(key1)) {
                            thk.setWhole_samePlate15_ds_grade(value1);
                        } else if ("whole_samePlate15_ws_grade".equals(key1)) {
                            thk.setWhole_samePlate15_ws_grade(value1);
                        } else if ("thickness".equals(key1)) {
                            thk.setThickness(value1);
                        }
                    }
                }
            }
            return ResultData.ResultDataSuccess(thk);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    public ResultData getPlanwtPredictData(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        JSONObject condition = jsonObject.getJSONObject("condition");
        List<Planwtpredict> list = planprodListDao.findPlanwtpredict((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
        Integer count=planprodListDao.findPlanwtpredictcount(condition);
        return ResultData.ResultDataSuccess(list,count);
    }

    public ResultData getPlanwtPredictDatabyhotciolnum(JSONObject json) {
        String hotcoilnum = json.getString("hotcoilnum");
        List<PlanprdList> planprdList = planProdListMapper.findPlanprdListbyciolnum(hotcoilnum);
        Double timesum = 0.0;
        Double wtsum = 0.0;
        Integer coilsum = 0;
        boolean flag = false;
        String hot_coil_id = "";
        Planwtpredict planwtpredict = new Planwtpredict();
        for (PlanprdList entity : planprdList) {
            if (timesum < 28800) {
                coilsum++;
                timesum += entity.getDuration();
                wtsum += entity.getIn_mat_wt();
                hot_coil_id = entity.getHot_coil_id();
                flag = false;
            } else {
                flag = true;
            }
        }
        if (flag) {
            planwtpredict.setRemark("根据当前已下发未生产的计划推算八小时,开始卷号" + json + ",结束卷号：" + hot_coil_id);
        } else {
            planwtpredict.setRemark("根据当前已下发未生产的计划推算未到8小时,开始卷号" + json + ",结束卷号：" + hot_coil_id);
        }

        planwtpredict.setLine("C622");
        planwtpredict.setSum_wt(wtsum / 1000);
        planwtpredict.setSum_coil(coilsum);
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = dateFormat.format(date);
        int foo = Integer.parseInt(dateString.substring(11, 13));
        if (foo == 0) {
            planwtpredict.setShift(1);
        } else if (foo == 8) {
            planwtpredict.setShift(2);
        } else if (foo == 16) {
            planwtpredict.setShift(3);
        }
        planwtpredict.setDate1(dateString);

        planwtpredict.setCreatetime(new Date());
        return ResultData.ResultDataSuccess(planwtpredict);
    }

}
